// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function() { // 通用预处理器

	},
	/**
	 * 开启考试
	 * @param {string} exams_date 考试时间
	 * @param {string} question_storehouse_id 题库ID
	 * @returns {object} 返回值描述
	 */
	async openExam(exams_date, question_storehouse_id) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		return await dbJQL.collection('topic-exams').add({
			exams_date,
			question_storehouse_id
		})
	},
	/**
	 * 上传考试答案
	 * @param {Array} arrs 答题内容
	 * @returns {object} 返回值描述
	 */
	async answersForExam(arrs) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		return await dbJQL.collection('topic-answers').add(arrs)
	},
	/**
	 * 获取考试
	 * @param {number} pageNo 页码
	 * @param {number} pageSize 每页多少条数据
	 * @returns {object} 返回值描述
	 */
	async exam_list(pageNo, pageSize) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate

		return dbJQL.collection('topic-exams').aggregate()
			.match({
				user_id: dbJQL.getCloudEnv('$cloudEnv_uid')
			})
			.lookup({
				from: 'questions-storehouse',
				let: {
					storehouse_id: "$question_storehouse_id"
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(["$_id", "$$storehouse_id"]))).done(),
				as: 'storehouse'
			})
			.lookup({
				from: 'topic-answers',
				let: {
					topic_exams_id: "$_id"
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(["$topic_exams_id", "$$topic_exams_id"])))
					.done(),
				as: 'topics'
			})
			.project({
				exams_date: 1,
				topics: 1,
				storehouse: $.arrayElemAt(['$storehouse', 0])
			})
			.sort("exams_date", "desc")
			.skip((pageNo - 1) * pageSize).limit(pageSize).end()
	},
	/**
	 * 获取考试详情
	 * @param {string} id 考试id
	 * @returns {object} 返回值描述
	 */
	async exam_detail(_id) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate
		return await dbJQL.collection('topic-exams').aggregate()
			.match({
				_id
			})
			.lookup({
				from: 'questions-storehouse',
				let: {
					question_storehouse_id: "$question_storehouse_id"
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(["$_id", "$$question_storehouse_id"])))
					.project({
						title: 1,
						year: 1
					}).done(),
				as: 'storehouse'
			})
			.lookup({
				from: 'topic-answers',
				let: {
					topic_exams_id: _id
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(["$topic_exams_id", "$$topic_exams_id"])))
					.lookup({
						from: 'questions-topic',
						let: {
							topic_id: "$questions_topic_id"
						},
						pipeline: $.pipeline().match(dbCmd.expr($.eq(['$_id', '$$topic_id'])))
							.done(),
						as: 'topic'
					})
					.project({
						user_answer: 1,
						is_correct: 1,
						answer_content: 1,
						topic: $.arrayElemAt(['$topic', 0])
					})
					.done(),
				as: 'topics'
			})
			.project({
				storehouse: $.arrayElemAt(['$storehouse', 0]),
				topics: 1,
				answer: 1,
				is_correct: 1
			})
			.end()
	},
	/* 获取考试错题解析 */
	async exam_error_analysis(_id) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate
		return await dbJQL.collection('topic-exams').aggregate()
			.match({
				_id
			})
			.lookup({
				from: 'topic-answers',
				let: {
					topic_exams_id: _id
				},
				pipeline: $.pipeline().match(dbCmd.expr(
						$.and([
							$.eq(["$topic_exams_id", "$$topic_exams_id"]),
							$.eq(["$is_correct", false])
						])))
					.lookup({
						from: 'questions-topic',
						let: {
							topic_id: "$questions_topic_id"
						},
						pipeline: $.pipeline().match(dbCmd.expr($.eq(['$_id', '$$topic_id'])))
							.lookup({
								from: 'topic-options',
								let: {
									topic_id: '$_id'
								},
								pipeline: $.pipeline().match(dbCmd.expr(
									$.eq(['$topic_id', '$$topic_id'])
								)).done(),
								as: 'options'
							})
							.done(),
						as: 'topic'
					})
					.project({
						user_answer: 1,
						is_correct: 1,
						topic: $.arrayElemAt(['$topic', 0])
					})
					.done(),
				as: 'topics'
			}).end()
	}
}